{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from matplotlib import pyplot as plt\n",
    "\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei']     # 显示中文\n",
    "# 为了坐标轴负号正常显示。matplotlib默认不支持中文，设置中文字体后，负号会显示异常。需要手动将坐标轴负号设为False才能正常显示负号。\n",
    "plt.rcParams['axes.unicode_minus'] = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "all_data = pd.read_csv('../data/mbti_weibo_data_cantrain1500.csv')\n",
    "all_data.head(1)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "target_data = all_data.iloc[:,1:7]\n",
    "target_data.head(1)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "features_data0 = all_data.iloc[:,7:2002]\n",
    "features_data0.head(1)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "from sklearn.feature_selection import (SelectKBest, chi2, SelectPercentile, SelectFromModel, SequentialFeatureSelector, SequentialFeatureSelector)\n",
    "\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "scaler = MinMaxScaler()\n",
    "\n",
    "y_EI = target_data[\"EI\"]\n",
    "y_NS = target_data[\"NS\"]\n",
    "y_TF = target_data[\"TF\"]\n",
    "y_JP = target_data[\"JP\"]\n",
    "\n",
    "y_list = {\"y_EI\":y_EI,\"y_NS\":y_NS,\"y_TF\":y_TF,\"y_JP\":y_JP}\n",
    "X  = features_data0"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LogisticRegression\n",
    "\n",
    "def trainModelTest(X,y):\n",
    "    X  = scaler.fit_transform(X)  #对自变量X做标准化处理\n",
    "    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)\n",
    "\n",
    "    # 创建逻辑回归模型\n",
    "    log = LogisticRegression()\n",
    "    log.fit(X_train, y_train)\n",
    "    score_l = log.score(X_test,y_test)\n",
    "    return score_l"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# import modules\n",
    "from sklearn.feature_selection import (SelectKBest, chi2, SelectPercentile, SelectFromModel, SequentialFeatureSelector, SequentialFeatureSelector)\n",
    "y_names =  [\"y_EI\",\"y_NS\",\"y_TF\",'y_JP']\n",
    "\n",
    "for yname in y_names:\n",
    "    # select K best features\n",
    "    bestrsf = 0\n",
    "    y = y_list[yname]\n",
    "    for k in range(20):\n",
    "        print(k)\n",
    "        X_best = SelectKBest(chi2, k=k).fit_transform(X,y)\n",
    "\n",
    "        # number of best features\n",
    "        X_best.shape[1]\n",
    "\n",
    "        rsfs = trainModelTest(X,y)\n",
    "        if rsfs >bestrsf:\n",
    "            bestrsf = rsfs\n",
    "    print(bestrsf)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}